"""
演示异常捕获
"""

#基本捕获异常
try:
    f = open("D:/text1.txt","r",encoding="UTF-8")

except:
    print("出现异常了,文件没有创建,我将open的模式改成w的模式去打开")
    f = open("D:/text1.txt", "w", encoding="UTF-8")

# 捕获多个异常
try:
    1/0
    print(name)
except (NameError,ZeroDivisionError) as e:
    print(e)
    print("出现了变量未定义的异常")
    print("出现了除数为0的异常")

#未正确设置捕获异常类型,将无法捕获异常

#捕获所有 异常
try:
    f = open("D:/text1.txt", "r", encoding="UTF-8")
except Exception as e:
    print("出现异常了")
else:#没有出现异常执行的语句
    print("没有出现异常,我好高兴.")
finally:#不管出不出现异常finally都要执行,一般用于文件的关闭操作
    print("我是finally,出不出现异常我都要执行")
    f.close()